﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Threading;

namespace LaunchApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = "..\\..\\..\\AppTest\\bin\\debug\\AppTest.exe";
            string formname = "AppTest.Form1";
            LaunchApp(filepath, formname);

            Console.WriteLine("....");
            Console.ReadLine();
        }


        private static Form LaunchApp(string fileName, string formName)
        {

            Assembly assembly = Assembly.LoadFrom(fileName);
            Type t = assembly.GetType(formName);

            Form form = (Form)assembly.CreateInstance(t.FullName);
            //Application.Run(form);


            AppState ap = new AppState(form);
            ThreadStart ts = new ThreadStart(ap.Runapp);
            Thread thread = new Thread(ts);
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Start();
            return form;

        }
    }

    class AppState
    {
        public readonly Form form;
        public AppState(Form form)
        {
            this.form = form;
        }

        public void Runapp()
        {
            Application.Run(form);
        }
    }
}
